﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;

namespace LunwenYun.Infrastructure.Entities.Repository
{
    [Table("T_Article")]
    public class T_Article : IEntity<T_Article>
    {
        #region
        [Key]
        [DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }
        /// <summary>
        /// 用户名
        /// </summary>
        [Column(TypeName = "varchar")]
        [StringLength(64)]
        public string UserName { get; set; }
        /// <summary>
        /// Aptitude
        /// </summary>
        [Column(TypeName="varchar")]
        [StringLength(64)]
        public string Aptitude { get; set; }
        /// <summary>
        /// Language
        /// </summary>
        public int Language { get; set; }
        /// <summary>
        /// 行业分类
        /// </summary>
        public int IndType { get; set; }
        public int SubIndType { get; set; }
        /// <summary>
        /// 稿件标题
        /// </summary>
        [Column(TypeName = "nvarchar")]
        [StringLength(128)]
        public string Title { get; set; }
        /// <summary>
        /// 稿件标题_EN
        /// </summary>
        [Column(TypeName = "varchar")]
        [StringLength(256)]
        public string Title_EN { get; set; }
        /// <summary>
        /// 摘要
        /// </summary>
        [Column(TypeName = "text")]
        public string Abstract { get; set; }
        /// <summary>
        /// 摘要_EN
        /// </summary>
        [Column(TypeName = "text")]
        public string Abstract_EN { get; set; }
        /// <summary>
        /// 关键词
        /// </summary>
        [Column(TypeName = "nvarchar")]
        [StringLength(256)]
        public string KeyWords { get; set; }
        /// <summary>
        /// 关键词_EN
        /// </summary>
        [Column(TypeName = "varchar")]
        [StringLength(512)]
        public string KeyWords_EN { get; set; }
        public DateTime CreateTime { get; set; }
        public DateTime ExpireTime { get; set; }
        public int PageSize { get; set; }
        /// <summary>
        /// 联系方式
        /// </summary>
        [Column(TypeName = "nvarchar")]
        [StringLength(128)]
        public string ContactInfo { get; set; }

        [Column(TypeName = "nvarchar")]
        [StringLength(32)]
        public string QQ { get; set; }
        /// <summary>
        /// 费用范围
        /// </summary>
        [Column(TypeName = "varchar")]
        [StringLength(128)]
        public string PriceRange { get; set; }
        [Column(TypeName="bit")]
        public bool AcceptShortMessage { get; set; }
        public int Status { get; set; }
        [Column(TypeName = "bit")]
        public bool IsDelete { get; set; }
        #endregion

        public override T_Article Find(string key)
        {
            int id = Int32.Parse(key);
            return Find(c => c.Id == id);
        }

        public override T_Article Find(System.Linq.Expressions.Expression<Func<T_Article, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.Article.FirstOrDefault(where);
                if (item != null)
                {
                    this.Abstract = item.Abstract;
                    this.Abstract_EN = item.Abstract_EN;
                    this.AcceptShortMessage = item.AcceptShortMessage;
                    this.Aptitude = item.Aptitude;
                    this.CreateTime = item.CreateTime;
                    this.ExpireTime = item.ExpireTime;
                    this.Id = item.Id;
                    this.IndType = item.IndType;
                    this.SubIndType = item.SubIndType;
                    this.IsDelete = item.IsDelete;
                    this.KeyWords = item.KeyWords;
                    this.KeyWords_EN = item.KeyWords_EN;
                    this.PageSize = item.PageSize;
                    this.PriceRange = item.PriceRange;
                    this.Title = item.Title;
                    this.Title_EN = item.Title_EN;
                    this.UserName = item.UserName;
                    this.ContactInfo = item.ContactInfo;
                    this.QQ = item.QQ;
                    this.Language = item.Language;
                    this.Status = item.Status;
                    return this;
                }
                return null;
            }
        }

        public override bool Any(System.Linq.Expressions.Expression<Func<T_Article, bool>> where)
        {
            using (Context ctx = new Context())
            {
                return ctx.Article.Any();
            }
        }
    }
}
